<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0"
    xmlns:date="http://exslt.org/dates-and-times" exclude-result-prefixes="date">

    <xsl:param name="formal.object.autolabel">0</xsl:param>
    <!-- same as section.autolabel, but for formal objects. used by formal.object.heading -->

    <xsl:variable name="doc.ownerpassword">
        <xsl:value-of select="$doc.date"/>
    </xsl:variable>

    <xsl:variable name="doc.date">
        <!-- FBA: means edition date.
            If not present, feel free to use the publication date. -->
        <xsl:choose>
            <!-- date element used if present -->
            <xsl:when test="normalize-space(//bookinfo/date)">
                <xsl:value-of select="//bookinfo/date"/>
            </xsl:when>
            <xsl:when test="normalize-space(//info[1]/date)">
                <xsl:value-of select="//info[1]/date"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$doc.pubdate"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="doc.pubdate">
        <!-- FBA: pubdate means date of publication.
            If not present, feel free to use the current date. -->
        <xsl:choose>
            <xsl:when test="normalize-space(//pubdate)">
                <!-- pubdate element used if present -->
                <xsl:value-of select="//pubdate[1]"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="datetime.format">
                    <xsl:with-param name="date" select="date:date-time()"/>
                    <xsl:with-param name="format" select="'B Y'"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:variable>

    <xsl:variable name="doc.procdate">
        <!-- FBA: procdate means date of processing. -->
        <xsl:call-template name="datetime.format">
            <xsl:with-param name="date" select="date:date-time()"/>
            <xsl:with-param name="format" select="'x'"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="doc.info">
        <xsl:value-of
            select="normalize-space(
            concat(
            $doc.orgname,
            ' ',
            $doc.productname,
            ' ',
            $doc.productnumber,
            ' — ',
            $doc.title
            )
            )"/>
        <xsl:if test="$doc.edition != ''">
            <xsl:value-of select="concat(' ',normalize-space($doc.edition))"/>
        </xsl:if>
    </xsl:variable>

    <xsl:variable name="doc.info.compact"
        select="concat($doc.title,': ',$doc.productname, ' ', $doc.productnumber)"/>

    <xsl:variable name="doc.filename">
        <xsl:value-of
            select="concat(  $doc.type, '_' , $doc.productname.trim, '_', $doc.productnumber )"/>
        <xsl:if test="$doc.edition != ''">
            <xsl:value-of select="concat('_',$doc.edition.trim)"/>
        </xsl:if>

    </xsl:variable>

    <xsl:variable name="doc.type">
        <xsl:choose>
            <xsl:when test="contains($doc.title.trim, 'operationsmanual')">iom</xsl:when>
            <xsl:when test="contains($doc.title.trim, 'installationmanual')">iom</xsl:when>
            <xsl:when test="contains($doc.title.trim, 'certificationguide')">cg</xsl:when>
            <xsl:when test="contains($doc.title.trim, 'trainingmanual')">cg</xsl:when>
            <xsl:when test="contains($doc.title.trim, 'userguide')">ug</xsl:when>
            <xsl:when test="contains($doc.title.trim, 'referenceguide')">ug</xsl:when>
            <xsl:when test="contains($doc.title.trim, 'quickstartguide')">ug</xsl:when>
            <xsl:when test="contains($doc.title.trim, 'releasenotes')">ug</xsl:when>
            <xsl:otherwise>doc2008</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="doc.title.trim">
        <xsl:call-template name="string.lower">
            <xsl:with-param name="string" select="translate($doc.title,' -_|','')"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="doc.edition">
        <xsl:choose>
            <!-- check for edition element-->
            <xsl:when test="//bookinfo/edition">
                <xsl:value-of select="//bookinfo/edition"/>
            </xsl:when>

            <xsl:when test="//info[1]/edition">
                <xsl:value-of select="//info[1]/edition"/>
            </xsl:when>
        </xsl:choose>

        <!-- <xsl:variable name="first_info_element" select="//*[contains(local-name(),'info')][1]/> -->

        <xsl:if test="//*[1]/@status='draft'">
            <xsl:text> </xsl:text>
            <!-- don't forget to normalize the output string -->
            <xsl:text>[</xsl:text>
            <xsl:call-template name="gentext">
                <xsl:with-param name="key">Draft</xsl:with-param>
            </xsl:call-template>
            <xsl:text>]</xsl:text>
        </xsl:if>
    </xsl:variable>

    <xsl:variable name="doc.edition.trim">
        <xsl:call-template name="string.lower">
            <xsl:with-param name="string" select="translate($doc.edition,' -_| ','')"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="doc.issue-string">
        <xsl:text>Issued by</xsl:text>
    </xsl:variable>

    <xsl:variable name="doc.orgname">
        <xsl:choose>
            <!-- orgname -->
            <xsl:when test="//info[1]/orgname">
                <xsl:value-of select="//info[1]/orgname"/>
            </xsl:when>
            <xsl:when test="//bookinfo/orgname">
                <xsl:value-of select="//bookinfo/orgname"/>
            </xsl:when>
            <xsl:when test="//articleinfo[1]/orgname">
                <xsl:value-of select="//articleinfo[1]/orgname"/>
            </xsl:when>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="doc.productname">
        <xsl:choose>
            <!-- productname -->
            <xsl:when test="//info[1]/productname">
                <xsl:value-of select="//info[1]/productname"/>
            </xsl:when>
            <xsl:when test="//bookinfo/productname">
                <xsl:value-of select="//bookinfo/productname"/>
            </xsl:when>
            <xsl:when test="//articleinfo[1]/productname">
                <xsl:value-of select="//articleinfo[1]/productname"/>
            </xsl:when>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="doc.productname.trim">
        <xsl:call-template name="string.lower">
            <xsl:with-param name="string" select="translate($doc.productname,' -_| ','')"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="doc.productnumber">
        <xsl:choose>
            <!-- product number -->
            <xsl:when test="//info[1]/productnumber">
                <xsl:value-of select="//info[1]/productnumber"/>
            </xsl:when>
            <xsl:when test="//bookinfo/productnumber">
                <xsl:value-of select="//bookinfo/productnumber"/>
            </xsl:when>
            <xsl:when test="//articleinfo[1]/productnumber">
                <xsl:value-of select="//articleinfo[1]/productnumber"/>
            </xsl:when>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="doc.publishername">
        <xsl:choose>
            <xsl:when test="//publishername[1]">
                <xsl:value-of select="//publishername[1]"/>
            </xsl:when>
            <!-- If publishername is absent, fall back to default. -->
            <xsl:otherwise>
                <xsl:value-of select="concat('the ',$doc.orgname,' Documentation Department')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="doc.releaseinfo">
        <xsl:choose>
            <!-- document's releaseinfo is prioritary. -->
            <xsl:when test="//releaseinfo[1]">
                <xsl:value-of select="//releaseinfo[1]"/>
            </xsl:when>
            <!-- If releaseinfo absent, fall back to publishername (= 99.99 % situations).-->
            <xsl:otherwise>
                <xsl:value-of select="concat($doc.issue-string,' ',$doc.publishername)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="doc.title">
        <!-- document title -->
        <xsl:choose>
            <xsl:when test="//info[1]/title">
                <xsl:value-of select="//info[1]/title"/>
            </xsl:when>
            <xsl:when test="//bookinfo/title">
                <xsl:value-of select="//bookinfo/title"/>
            </xsl:when>
            <xsl:when test="//articleinfo[1]/title">
                <xsl:value-of select="//articleinfo[1]/title"/>
            </xsl:when>
            <xsl:when test="//title[1]">
                <xsl:value-of select="//title[1]"/>
            </xsl:when>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="doc.header-bitmap.left"
        select="concat($path.deco,'header-left-',$doc.type,'.png')"/>

    <xsl:variable name="doc.header-bitmap.right"
        select="concat($path.deco,'header-right-',$doc.type,'.png')"/>

    <xsl:variable name="doc.header-bitmap"
        select="concat($path.deco,'header-center-',$doc.type,'.png')"/>

    <xsl:variable name="company.logo-vector" select="concat($path.logos,'company.pdf')"/>

    <xsl:variable name="company.logo-bitmap.dimensions">122x82</xsl:variable>

    <xsl:variable name="company.logo-bitmap"
        select="concat($path.logos,'company-',$company.logo-bitmap.dimensions,'.png')"/>

    <xsl:variable name="doc.conformance">
        <!-- FIXME: could better be something like 'audience' -->
        <xsl:call-template name="string.lower">
            <xsl:with-param name="string"
                select="translate(//node()[contains(local-name(),'info')][1]/@conformance,' -_|','')"
            />
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="doc.footer.text-indent.length"/>

    <xsl:variable name="doc.product-cover">
        <xsl:choose>
            <xsl:when test="$doc.conformance = 'backoffice'">
                <xsl:value-of select="concat($path.covers,'backOffice.jpg')"/>
            </xsl:when>
            <xsl:when test="$doc.conformance = 'clientconnectivity'">
                <xsl:value-of select="concat($path.covers,'clientConnectivity.jpg')"/>
            </xsl:when>
            <xsl:when test="$doc.conformance = 'marketconnectivity'">
                <xsl:value-of select="concat($path.covers,'marketConnectivity.jpg')"/>
            </xsl:when>
            <xsl:when test="$doc.conformance = 'marketdata'">
                <xsl:value-of select="concat($path.covers,'marketData.jpg')"/>
            </xsl:when>
            <xsl:when test="$doc.conformance = 'ordermanagement'">
                <xsl:value-of select="concat($path.covers,'orderManagement.jpg')"/>
            </xsl:when>
            <xsl:when test="$doc.conformance = 'posttrademanagement'">
                <xsl:value-of select="concat($path.covers,'postTradeManagement.jpg')"/>
            </xsl:when>
            <xsl:when test="$doc.conformance = 'services'">
                <xsl:value-of select="concat($path.covers,'services.jpg')"/>
            </xsl:when>
            <xsl:when test="$doc.conformance = 'trading'">
                <xsl:value-of select="concat($path.covers,'trading.jpg')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat($path.covers,'blank.png')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="path.images">
        <xsl:choose>
            <xsl:when test="$use.local.graphics.path">images/mercury/</xsl:when>
            <xsl:otherwise>http://yoursite.com/stylesheets/images/mercury/</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="path.covers" select="concat($path.images,'covers/')"/>

    <xsl:variable name="path.deco" select="concat($path.images,'decoration/')"/>

    <xsl:variable name="path.logos" select="concat($path.images,'logos/')"/>

    <!-- bases de couleurs -->

    <xsl:variable name="title.font.color">
        <xsl:choose>
            <xsl:when test="$doc.type='iom'">
                <xsl:value-of select="$doc.color.iom-blue"/>
            </xsl:when>
            <xsl:when test="$doc.type='ug'">
                <xsl:value-of select="$doc.color.ug-green"/>
            </xsl:when>
            <xsl:when test="$doc.type='cg'">
                <xsl:value-of select="$doc.color.cg-marroon"/>
            </xsl:when>
            <xsl:when test="$doc.type='doc2008'">
                <xsl:value-of select="$doc.color.doc2008-green"/>
            </xsl:when>
            <xsl:otherwise>black</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="title.font.color2">
        <xsl:choose>
            <xsl:when test="$doc.type='iom'">
                <xsl:value-of select="$doc.color.iom-lightBlue"/>
            </xsl:when>
            <xsl:when test="$doc.type='ug'">
                <xsl:value-of select="$doc.color.ug-lightGreen"/>
            </xsl:when>
            <xsl:when test="$doc.type='cg'">
                <xsl:value-of select="$doc.color.cg-orange"/>
            </xsl:when>
            <xsl:when test="$doc.type='doc2008'">
                <xsl:value-of select="$doc.color.doc2008-lightGreen"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$doc.color.gray"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <!-- examples -->

    <xsl:variable name="doc.color.doc2008-green">#0f4b19</xsl:variable>

    <xsl:variable name="doc.color.doc2008-lightGreen">#99d01e</xsl:variable>

    <xsl:variable name="doc.color.blue">#006699</xsl:variable>

    <xsl:variable name="doc.color.green">#009966</xsl:variable>

    <xsl:variable name="doc.color.gray">#B3B3B3</xsl:variable>

    <xsl:variable name="doc.color.iom-blue">#0060A0</xsl:variable>

    <xsl:variable name="doc.color.iom-lightBlue">#80B6D7</xsl:variable>

    <xsl:variable name="doc.color.cg-marroon">#804000</xsl:variable>

    <xsl:variable name="doc.color.cg-orange">#FF80B0</xsl:variable>

    <xsl:variable name="doc.color.ug-green">#009865</xsl:variable>
    <!-- rgb(0,152,101)-->

    <xsl:variable name="doc.color.ug-lightGreen">#B1DFD0</xsl:variable>
    <!-- rgb(177,223,208)-->

</xsl:stylesheet>
